<?php

add_filter('wpjam_pages', 'wpjam_add_grab_mp_admin_pages');
function wpjam_add_grab_mp_admin_pages($wpjam_pages){

	$wpjam_pages['posts']['subs']['grab-mp'] = array(
		'menu_title'	=>'抓取图文',
		'function'		=> 'wpjam_grab_mp_article'
	);

	return $wpjam_pages;
}

function wpjam_grab_mp_article(){
	global $current_admin_url;

	$categories = get_categories(array(
		'orderby'	=> 'count',
		'order'		=> 'DESC'
	));

	$category_options = array();

	foreach ($categories as $category) {
		$category_options[$category->cat_ID] = $category->cat_name;
	}

	$form_fields 	= array(
		'mp_url'		=> array('title'=>'图文链接',		'type'=>'url',		'class'=>'large-text' ),
		'featured_image'=> array('title'=>'特色图片',		'type'=>'checkbox',	'description'=>'将微信图文的头图作为文章的特色图片' ),
		'category'		=> array('title'=>'分类',		'type'=>'radio',	'options'=>$category_options ),
	);

	$nonce_action = 'weixin-grab-mp-article';

	if( $_SERVER['REQUEST_METHOD'] == 'POST' ){
		$data = wpjam_get_form_post($form_fields, $nonce_action);

		if($mp_url = $data['mp_url']){
			$mp_url		= remove_query_arg(array('chksm','scene','srcid','mpshare'),$mp_url);
			$mp_url		= str_replace('%3D', '=', $mp_url);
			$article	= weixin_robot_parse_mp_article($mp_url);

			if(is_wp_error($article)){
				wpjam_admin_add_error($article->get_error_message(), 'error');
			}else{
				$featured_image = $data['featured_image'];
				$category 		= $data['category'];

				$post_content	= preg_replace('/<img .*? data-src="(.*?)" .*?>/i', '[wximg]\1[/wximg]', $article['content']);

				$post_content_preg	= preg_replace('/<iframe class="video_iframe" .*? data-src=".*vid=(.*?)&.*".*?><\/iframe>/i', '[qqv]https://v.qq.com/iframe/preview.html?vid=\1[/qqv]', $post_content);

				$post_content	= (is_null($post_content_preg))?$post_content:$post_content_preg;

				$post_content_preg	= preg_replace('/<iframe class="video_iframe" .*? data-src=".*vid=(.*?)".*?><\/iframe>/i', '[qqv]https://v.qq.com/iframe/preview.html?vid=\1[/qqv]', $post_content);

				$post_content	= (is_null($post_content_preg))?$post_content:$post_content_preg;

				$post_id = wp_insert_post(array(
					'post_title'	=> $article['title'],
					'post_excerpt'	=> $article['digest'],
					'post_content'	=> $post_content,
					'post_status'	=> 'draft'
				));

				if(is_wp_error($post_id)){
					wpjam_admin_add_error($post_id->get_error_message(), 'error');
				}elseif(!$post_id){
					//
				}else{
					wp_set_post_categories( $post_id, array($category));
					// if(strpos($mp_url, 'MjM5NzAwNzMyMA==')){	// 科技每日推送的，就保留微信的链接
					update_post_meta( $post_id, 'weixin_url', $mp_url);
					// }
					if($featured_image){
						$thumb_url = $article['thumb_url'];

						$file_array = array();
						$file_array['name'] 	= md5($thumb_url).'.jpg';
						$file_array['tmp_name']	= $tmpfname = WP_CONTENT_DIR.'/uploads/weixin/media/temp/'.md5($thumb_url).'.jpg';

						wpjam_remote_request($thumb_url, array('method'	=> 'file', 'stream'=>true, 'filename'=>$tmpfname, 'need_json_decode'=>false));
						
						$thumbnail_id = media_handle_sideload($file_array, $post_id, $article['title']);
						set_post_thumbnail($post_id, $thumbnail_id);
						// unlink($tmpfname);
					}
					$redirect_to = admin_url('post.php?post='.$post_id.'&action=edit');	
					wp_redirect($redirect_to);
				}
			}
		}else{
			wpjam_admin_add_error('你没有输入图文链接！','error');
		}
	}

	?>
	<h2>抓取图文</h2>

	<?php wpjam_form($form_fields, $current_admin_url, $nonce_action, '抓取'); ?>


	<?php
}